package com.ct.businesslogic.map.impl;

import com.ct.businesslogic.map.MapCarTrackerLogic;
import java.util.List;
import java.util.LinkedList;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.springframework.core.io.Resource;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 23.08.2009
 * Time: 14:17:49
 * To change this template use File | Settings | File Templates.
 */
public class MapCarTrackerLogicImpl implements MapCarTrackerLogic {
    private Resource carsInfoSource;
    private Resource destinationsSource;
    
    public Resource getCarsInfoSource() {
        return carsInfoSource;
    }

    public void setCarsInfoSource(Resource carsInfoSource) {
        this.carsInfoSource = carsInfoSource;
    }

    public Resource getDestinationsSource() {
        return destinationsSource;
    }

    public void setDestinationsSource(Resource destinationsSource) {
        this.destinationsSource = destinationsSource;
    }


    public List<Long> getCarsIds() throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(getCarsInfoSource().getInputStream()));
        String line;
        LinkedList<Long> numbers = new LinkedList<Long>();
        while((line = in.readLine()) != null){
            String[] carDetails = line.split(";");
            for(int i = 0; i < carDetails.length; i++){
                carDetails[i] = carDetails[i].trim();
            }
            numbers.add(new Long(carDetails[0]));
        }
        in.close();
        return numbers;
    }

    public String findCarDestinationById(int carId) throws Exception{

        BufferedReader in = new BufferedReader(new InputStreamReader(getCarsInfoSource().getInputStream()));
        String line;
        String destination = null;
        while((line = in.readLine()) != null){

            String[] destData = line.split(";");
            for(int i = 0; i < destData.length; i++){
                destData[i] = destData[i].trim();
            }
            if(Integer.parseInt(destData[0]) == carId){
                destination = destData[1];
            }
        }
        in.close();

        return destination;
    }
//
//    public static void main(String[] args){
//        double defaultLon = 37.6155556;
//        double defaultLat = 55.7522222;
//        for(int i = 0; i < 10; i++){
//            double offset =  Math.random() * (i + 1) / 100;
//            System.out.println((defaultLat - offset) + "; " + (defaultLon - offset));
//        }
//
//    }
}
